import sys,os
from Config import *


indir = sys.argv[1]
genomeindex = sys.argv[2]
indir = os.path.abspath(indir)
inlist = []
for i in os.listdir(indir):
    if i.endswith("fastq.gz") or i.endswith("fq.gz"):
        inlist.append(os.path.join(indir, i))

print(inlist)
inlist.sort()
if not os.path.exists("align"):
    os.mkdir("align")
cmd_sh = f'''#!/bin/bash --login
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=8
#SBATCH --mem=20G
#SBATCH --job-name=autofastp
#SBATCH --time=40:00:00
#SBATCH --partition=general
#SBATCH --account=a_qaafi_genomics
#SBATCH -o allhic.slurm.out
#SBATCH -e allhic.slurm.error\n
'''         
for i in range(0, len(inlist), 2):
    fq1 = inlist[i]
    fq2 = inlist[i+1]
    prefix = fq1.split("/")[-1].split(".")[0]
    cmd = f"{hisat2} -p 60 -x {genomeindex} -1 {fq1} -2 {fq2} -S align/{prefix}.sam\n"
    cmd += f"{samtools} view -bS align/{prefix}.sam > align/{prefix}.bam\n"
    cmd += f"{samtools} sort align/{prefix}.bam -o align/{prefix}.sort.bam\n"
    cmd += f"{samtools} index align/{prefix}.sort.bam\n"
    slum = open(f"{prefix}.hisat.slum", "w")
    slum.write(cmd_sh)
    slum.write(cmd)
    slum.close()
    os.system(f"sbatch {prefix}.hisat.slum")

